package xyz.mumuwei.youthcourseserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.mumuwei.youthcourseserver.common.lang.Result;
import xyz.mumuwei.youthcourseserver.mapper.UserInfoMapper;
import xyz.mumuwei.youthcourseserver.pojo.UserInfo;
import xyz.mumuwei.youthcourseserver.service.UserInfoService;

import java.util.List;

/**
 * @author mumuwei
 * @since 2022-02-19
 */
@Service
@RequiredArgsConstructor
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    final UserInfoMapper infoMapper;

    /**
     * 根据角色创建用户信息
     * @param userInfo 用户信息，必须指定角色id
     * @return 返回受影响行数
     */
    @Override
    public Integer createInfo(UserInfo userInfo) {
        UserInfo phone = infoMapper.selectOne(new QueryWrapper<UserInfo>()
                .eq("phone", userInfo.getPhone()));
        if (phone != null){
            return -1;
        }
        return infoMapper.insert(userInfo);
    }

    /**
     * 根据id 修改用户信息
     *
     * @param userInfo 修改后的用户信息
     * @return 受影响行数
     */
    @Override
    public Integer updateInfo(UserInfo userInfo) {
        if(userInfo.getId() == null || userInfo.getId() == 0){
            return -1;
        }
        UserInfo info = infoMapper.selectById(userInfo.getId());
        if(!info.getRoleId().equals(userInfo.getRoleId())){
            return -1;
        }
        return infoMapper.updateById(userInfo);
    }

    /**
     * 根据课程id获取课程教师列表
     *
     * @param courseId 课程id
     * @return 教师列表
     */
    @Override
    public Result getByCourseId(Integer courseId) {
        List<UserInfo> list = infoMapper.getByCourseId(courseId);
        if (list.size() <= 0) {
            return Result.fail("查询有误！");
        }
        return Result.success(list);
    }
}
